/*
 * Copyright (c) 2025 Li Auto Inc. and its affiliates
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef INCLUDE_EDDS_RTPS_SECURITY_COMMON_AUTHTOKEN_H_
#define INCLUDE_EDDS_RTPS_SECURITY_COMMON_AUTHTOKEN_H_

#include <cstdint>
#include <limits>
#include <stdexcept>
#include <string.h>
#include <vector>

namespace evbs {
namespace ertps {
namespace rtps {

namespace security {

class AuthToken {
 public:
    AuthToken() { init(); }

    uint32_t& firstToken() { return first_token_; }

    uint32_t& authToken() {
        if (current_token_ == 0) {
            current_token_ = first_token_;
        }
        return current_token_;
    }

    void authToken(uint32_t token) { current_token_ = token; }

    uint64_t max_blocks_per_session = 0U;  // config when the token update
    uint64_t session_block_edge = 0U;

    std::pair<uint32_t, uint32_t> compute_authtoken(const std::vector<uint8_t>& key, std::vector<uint8_t>& data, const uint32_t block_id);

    std::pair<uint32_t, uint32_t> compute_authtoken(std::vector<uint8_t>& data, const uint32_t block_id);

    void reset();

 private:
    void init();

    uint32_t first_token_;
    uint32_t current_token_;
};

}  // namespace security
}  // namespace rtps
}  // namespace ertps
}  // namespace evbs

#endif  // INCLUDE_EDDS_RTPS_SECURITY_COMMON_AUTHTOKEN_H_
